var express = require('express');
var router = express.Router();
let { bookListModel, bookTypeModel, bookDationsModel } = require('../model/model_ysy');

/* GET home page. */
router.get('/bookList', async (req, res) => {
  let data = await bookListModel.find();
  res.send({
    code: 200,
    data
  })
})
// 书籍分类 
// router.get('/bookType', async (req, res) => {
//   let data = await bookTypeModel.find().lean()
//   let cateJson = {}
//   data.forEach(ele => {
//     cateJson[ele._id] = ele
//   })
//   let catelist = []
//   data.forEach(ele => {
//     if (!ele.pid) {
//       catelist.push(ele)
//     } else {
//       if (!cateJson[ele["pid"]]["childer"]) {
//         cateJson[ele["pid"]]["childer"] = []
//       }
//       cateJson[ele["pid"]]["childer"].push(ele)
//     }
//   })
//   res.send(catelist)
// })
// 书单推荐
router.get('/bookDations', async (req, res) => {
  let data = await bookDationsModel.find();
  res.send({
    code: 200,
    data
  })
})

// 书籍添加
router.post('/bookList_add', async (req, res) => {
  await bookListModel.create(req.body);
  res.send({ msg: '添加成功' })
})


// 书籍筛选
router.get('/look', async (req, res) => {
  let data = await bookListModel.find()
  let { title } = req.query
  console.log(title);
  if (title != '全部商品' && title != '') {
    data = data.filter(ele => ele.bookTitle.includes(title))
  }
  console.log(data);
  res.send({ data })
})
module.exports = router;
